<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="moneyList" highlight-current-row v-loading="listLoading"  style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="money" label="充值金额" width="200">
			</el-table-column>
			<el-table-column prop="gold" label="充值金币" width="200">
			</el-table-column>
			<!--<el-table-column prop="age" label="赠送金币" width="200">-->
			<!--</el-table-column>-->
			<el-table-column label="操作" min-width="250">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="sizes, total, prev, pager, next" :page-sizes="[15, 30, 50]" @current-change="handleCurrentChange" @size-change="handleSizeChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" :inline="true" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="充值面额" prop="money">
					<el-input v-model="editForm.money" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="充值金币" prop="gold">
					<el-input v-model="editForm.gold" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="赠送金币" prop="id">-->
					<!--<el-input v-model="editForm.name" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit(editForm.id)" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" :inline="true"  :rules="addFormRules" ref="addForm">
				<el-form-item label="充值金额" prop="money">
					<el-input v-model="addForm.money" auto-complete="off" type="number"></el-input>
				</el-form-item>
				<el-form-item label="充值金币" prop="gold">
					<el-input v-model="addForm.gold" auto-complete="off" type="number"></el-input>
				</el-form-item>
				<!--<el-form-item label="赠送金币" prop="id">-->
					<!--<el-input v-model="editForm.name" auto-complete="off"></el-input>-->
				<!--</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
                moneyList: [],
				total: 0,
				page: 1,
				pageSize:15,
				listLoading: false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					money: [
						{ required: true, message: '请输入充值金额', trigger: 'blur' }
					],
					gold: [
                        { required: true, message: '请输入充值金币', trigger: 'blur' }
                    ],
				},
				//编辑界面数据
				editForm: {
                    money:'',
                    gold:''
				},

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
                    money: [
                        { required: true, message: '请输入充值金额', trigger: 'blur' }
                    ],
                    gold: [
                        { required: true, message: '请输入充值金币', trigger: 'blur' }
                    ]
				},
				//新增界面数据
				addForm: {
					money:'',
					gold:'',
                    state:1
				}

			}
		},
		methods: {
            handleSizeChange(val) {
                this.pageSize = val
                this.getUsers(this.pageSize);
            },
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers(this.pageSize);
			},
			//获取用户列表
			getUsers(page) {
				this.$axios.get('/recharge-commodities?index='+this.page+'&size='+page,{},this.global().token).then((res)=>{
				    if (res.status == 200){
				        this.total = res.data.count
				        let money = res.data.data
                        this.moneyList = money.filter(function(item){
                            return (item.state == 1);
                        })
					}else{
                        this.$message({message: res.data, type: 'error'});
					}
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					row.state = 0
					this.$axios.patch('/recharge-commodity/'+row.id,row,this.global().token).then((res)=>{
					    if (res.status == 200){
                            this.$message({message: '删除成功', type: 'success'});
                            this.getUsers(this.pageSize);
                            this.listLoading = false;
						}else{
                            this.$message({message: res.data, type: 'error'});
						}
					})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					money:'',
					gold:'',
					state:1
				};
			},
			//编辑
			editSubmit: function (goldId) {
                this.$axios.patch('/recharge-commodity/'+goldId,this.editForm,this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.$message({message: '修改成功', type: 'success'});
                        this.editFormVisible = false;
                        this.getUsers(this.pageSize);
                    }else{
                        this.$message({message: res.data, type: 'error'});
                    }
                })
			},
			//新增
			addSubmit: function () {
			    this.addForm.money = parseInt(this.addForm.money)
                this.addForm.gold = this.addForm.gold + '金币'
                this.$axios.post('/recharge-commodity',this.addForm,this.global().token).then((res)=>{
                    if (res.status == 200){
                        this.$message({message: '添加成功', type: 'success'});
                        this.addFormVisible = false;
                        this.getUsers(this.pageSize);
                    }else{
                        this.$message({message: res.data, type: 'error'});
                    }
                })
			}
		},
		mounted() {
			this.getUsers(this.pageSize);
		}
	}

</script>

<style scoped>

</style>